package com.bootdo.acitvity.DDD.domain;
import com.bootdo.acitvity.DDD.domain.entity.AdjuestmentStock;
import com.bootdo.acitvity.DDD.domain.entity.ChannelLockedStock;
import com.bootdo.acitvity.DDD.domain.entity.ShareStock;
import com.bootdo.acitvity.DDD.domain.event.StockAdjustmentStrategy;
import com.bootdo.acitvity.DDD.domain.event.StockLockCreateStrategy;
import com.bootdo.acitvity.DDD.domain.event.StockLockUpdateStrategy;
import com.bootdo.acitvity.DDD.domain.event.StockUpdateContext;
import lombok.Data;
import lombok.Getter;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  聚合跟
 */
@Data
public class CenterBatch {

    // 值对象
    // 多个相关属性组合为一个概念整体
    // 没有标识符的对象，可以继承某一特性，叫作值对象
    // 若干个用于描述目的（行为）
    // 具有整体概念和不可修改的属性

    private final Long id;

    /**
     * 批次表主键ID--> 不同的ID代表不同的聚合根
     */
    @Getter
    private final String batchId;

    /**
     * 批次效期开始时间  --> 值对象
     */
    private Date batchValidateStartDate;

    /**
     * 批次效期结束时间  --> 值对象
     */
    private Date batchValidateEndDate;

    /**
     * 共享库存  --> 值对象
     */
    private ShareStock shareStock;

    /**
     * 渠道库存集合   --> 实体
     */
    private Map<Long, ChannelStock> channelStockMap = new HashMap<>();

    /**
     * 取消分配渠道库存集合   --> 实体
     */
    private Map<Long, ChannelStock> unassignedChannelStockMap = new HashMap<>();


    /**
     * 渠道取消分配  ----> 行为
     *
     * @param channelId
     * @return
     */
    public void assignChannel(Long channelId) {
        unassignedChannelStockMap.remove(channelId);
        channelStockMap.put(channelId, new ChannelStock(channelId));
    }

    /**
     * 获取渠道锁定库存  ----> 行为
     *
     * @param channelId
     * @return
     */
    public ChannelLockedStock getLockedStock(Long channelId) {
        return channelStockMap.get(channelId).getChannelLockedStock();
    }

    /**
     * 更新锁定库存  ----> 行为
     *
     * @param channelLockedStock
     */
    public void updateLockedStock(ChannelLockedStock channelLockedStock) {
        channelStockMap.get(channelLockedStock.getChannelId()).setChannelLockedStock(channelLockedStock);
    }

    /**
     * 添加锁定库存  ----> 行为
     *
     * @param channelLockedStock
     */
    public void addLockedStock(ChannelLockedStock channelLockedStock) {
        channelStockMap.get(channelLockedStock.getChannelId()).setChannelLockedStock(channelLockedStock);
    }

    /**
     * 判断该渠道是否有锁定库存  ---> 行为
     *
     * @param channelId
     * @return
     */
    public boolean isLockedAlready(Long channelId) {
        return channelStockMap.get(channelId).isLocked();
    }


    /**
     * 库存调整 --> 领域事件
     */
    public void adjustmentStock(Long stockAmount) {
        AdjuestmentStock stock = new AdjuestmentStock(stockAmount);
        StockUpdateContext.withStrategy(new StockAdjustmentStrategy()).updateStock(this, stock);
    }



    /**
     * 创建锁定库存 --> 领域事件
     *
     * @param flockStockNum 锁定库存数量
     * @param flockStockEndTime 锁定结束时间
     */
    public void lockStock(Long flockStockNum, Date flockStockEndTime) {
        ChannelLockedStock builder = ChannelLockedStock.getBuilder()
                .setStockAmount(flockStockNum)
                .setFlockStockEndTime(flockStockEndTime)
                .build();
        StockUpdateContext.withStrategy(new StockLockCreateStrategy()).updateStock(this, builder);
    }

    /**
     * 更新锁定库存 --> 领域事件
     *
     * @param flockStockNum 锁定库存数量
     * @param flockStockEndTime 锁定结束时间
     */
    public void updateStock(Long flockStockNum, Date flockStockEndTime) {
        ChannelLockedStock builder = ChannelLockedStock.getBuilder()
                .setStockAmount(flockStockNum)
                .setFlockStockEndTime(flockStockEndTime)
             .build();
        StockUpdateContext.withStrategy(new StockLockUpdateStrategy()).updateStock(this, builder);
    }



}
